﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace DataCodeDemo
{
    public partial class Form2 : Form
    {
        private string _enableServer;
        private string _localIP;
        private string _localPort;
        private string _triggerCharServer;
        private string _splitCharServer;
        private string _enableClient;
        private string _remoteIP;
        private string _remotePort;
        private string _triggerCharClient;
        private string _splitCharClient;
        private string _reConn;

        public Form2()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["EnableTcpServer"].Value = swTcpServer.Active ? "true" : "false";
            config.AppSettings.Settings["TcpServerIP"].Value = txtLocalIp.Text;
            config.AppSettings.Settings["TcpServerPort"].Value = txtLocalPort.Text;
            config.AppSettings.Settings["TcpServerTriggerChar"].Value = txtTriggerCharServer.Text;
            config.AppSettings.Settings["TcpServerSplitChar"].Value = txtSplitCharServer.Text;
            config.AppSettings.Settings["EnableTcpClient"].Value = swTcpClient.Active ? "true" : "false";
            config.AppSettings.Settings["TcpClientIP"].Value = txtRemoteIp.Text;
            config.AppSettings.Settings["TcpClientPort"].Value = txtRemotePort.Text;
            config.AppSettings.Settings["TcpClientTriggerChar"].Value = txtTriggerCharClient.Text;
            config.AppSettings.Settings["TcpClientSplitChar"].Value = txtSplitCharClient.Text;
            config.AppSettings.Settings["TcpClientReConn"].Value = chkReConn.Checked ? "true" : "false";
            //一定要记得保存，写不带参数的config.Save()也可以
            config.Save(ConfigurationSaveMode.Modified);
            //刷新，否则程序读取的还是之前的值（可能已装入内存）
            ConfigurationManager.RefreshSection("appSettings");
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
       

        private void Form2_Load(object sender, EventArgs e)
        {
            _enableServer = ConfigurationManager.AppSettings["EnableTcpServer"].ToString();
            _localIP = ConfigurationManager.AppSettings["TcpServerIP"].ToString();
            _localPort = ConfigurationManager.AppSettings["TcpServerPort"].ToString();
            _triggerCharServer = ConfigurationManager.AppSettings["TcpServerTriggerChar"].ToString();
            _splitCharServer = ConfigurationManager.AppSettings["TcpServerSplitChar"].ToString();
            _enableClient = ConfigurationManager.AppSettings["EnableTcpClient"].ToString();
            _remoteIP = ConfigurationManager.AppSettings["TcpClientIP"].ToString();
            _remotePort = ConfigurationManager.AppSettings["TcpClientPort"].ToString();
            _triggerCharClient = ConfigurationManager.AppSettings["TcpClientTriggerChar"].ToString();
            _splitCharClient = ConfigurationManager.AppSettings["TcpClientSplitChar"].ToString();
            _reConn = ConfigurationManager.AppSettings["TcpClientReConn"].ToString();

            txtLocalIp.Text = _localIP;
            txtLocalPort.Text = _localPort;
            txtTriggerCharServer.Text = _triggerCharServer;
            txtSplitCharServer.Text = _splitCharServer;
            swTcpServer.Active = _enableServer == "true";
            txtRemoteIp.Text = _remoteIP;
            txtRemotePort.Text = _remotePort;
            txtTriggerCharClient.Text = _triggerCharClient;
            txtSplitCharClient.Text = _splitCharClient;
            swTcpClient.Active = _enableClient == "true";
            chkReConn.Checked = _reConn == "true";
        }
    }
}
